/**
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *    
 *    Linking this library statically or dynamically with other modules 
 *    is making a combined work based on this library. Thus, the terms and
 *    conditions of the GNU General Public License cover the whole
 *    combination.
 *    
 *    As a special exception, the copyright holders of this library give 
 *    you permission to link this library with independent modules to 
 *    produce an executable, regardless of the license terms of these 
 *    independent modules, and to copy and distribute the resulting 
 *    executable under terms of your choice, provided that you also meet, 
 *    for each linked independent module, the terms and conditions of the 
 *    license of that module.  An independent module is a module which 
 *    is not derived from or based on this library.  If you modify this 
 *    library, you may extend this exception to your version of the 
 *    library, but you are not obligated to do so.  If you do not wish 
 *    to do so, delete this exception statement from your version.
 *
 *    Project: Financas Pessoais
 *    Author : Igor Salles
 *    Created: May 14, 2011
 */
package org.fp.logger;

import java.util.Properties;
import java.util.logging.Logger;

///**
// * A simple wrapper around JDK logging facilities.
// * 
// * @author Igor Salles
// * @since may 14, 2011
// * @created may 14, 2011
// */
//public class Jornal {
//    /* 
//     * esta eh uma classe que pretendo utilizar como singleton. a principio
//     * o debug do projeto sera enviado para arquivo e console, e mais tarde
//     * somente para arquivo, ja que uma das interfaces de usuaria sera a de
//     * texto. 
//     * Para classes singleton temos que:
//     *  - Deixe o seu construtor privado;
//     *  - Crie o atributo estático para a instância única da sua classe;
//     *  - Crie o método estático para obter essa instância. 
//     */
//    //instancia unica da classe
//    private static Jornal instance;
//    //variaves e campos da classe
//    //propriedades de configuracao do logger
//    private Logger jornalLogger;
//    
//    //Construtor unico da classe
//    private Jornal(){
//        //
//        }
//    //metodo estatico para obeter a instancia
//    public static synchronized Jornal getInstance(){
//        return new Jornal();
//    }
//    
//    public void configure(){
//        //
//        Properties props = new Properties();
//    }
//    
//}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
//    
//    
//    private static Logger jornalRecs;
//    
//    private Jornal(String name){
//        jornalRecs = Logger.getLogger(name);
//    }
//    
//    public static Logger getLogger(String name){
//        return jornalRecs;
//    }
//    
//    public static void configure(String filename) {
//        FileInputStream fis = null;
//        try {
//            fis = new FileInputStream(filename);
//
//            LogManager.getLogManager().readConfiguration(fis);
//        } catch (SecurityException ex) {
//            //
//        } catch (FileNotFoundException ex) {
//            //
//        } catch (IOException ex) {
//            //
//        } finally {
//            try {
//                fis.close();
//            } catch(IOException e) {
//                
//            }
//        }
//    }
//}


//public class Logger {
//
//    private String fileName;
//    private Properties properties;
//    private Priority priority;
//
//    /**
//     * Private constructor
//     */
//    private Logger() {
//        logger = this;
//    }
//
//    /**
//     * Level of logging, error or information etc
//     *
//     * @return level, int
//     */
//    public int getRegisteredLevel() {
//        int i = 0;
//        try {
//            InputStream inputstream = getClass().getResourceAsStream("Logger.properties");
//            properties.load(inputstream);
//            inputstream.close();
//            i = Integer.parseInt(properties.getProperty("**logger.registeredlevel**"));
//            if (i < 0 || i > 3) {
//                i = 0;
//            }
//        } catch (Exception exception) {
//            System.out.println("Logger: Failed in the getRegisteredLevel method");
//            exception.printStackTrace();
//        }
//        return i;
//    }
//
//    /**
//     * One file will be made daily. So, putting date time in file
//     * name.
//     *
//     * @param gc GregorianCalendar
//     * @return String, name of file
//     */
//    private String getFileName(GregorianCalendar gc) {
//        SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
//        String dateString = dateFormat1.format(gc.getTime());
//        String fileName = "C:\\prashant\\patterns\\log\\PatternsExceptionLog-" + dateString + ".txt";
//        return fileName;
//    }
//
//    /**
//     * A mechanism to log message to the file.
//     *
//     * @param p Priority
//     * @param message String
//     */
//    public void logMsg(Priority p, String message) {
//        try {
//            GregorianCalendar gc = new GregorianCalendar();
//            String fileName = getFileName(gc);
//            FileOutputStream fos = new FileOutputStream(fileName, true);
//            PrintStream ps = new PrintStream(fos);
//            SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, MMM d, yyyy 'at' hh:mm:ss a");
//            ps.println("<" + dateFormat2.format(gc.getTime()) + ">[" + message + "]");
//            ps.close();
//        } catch (IOException ie) {
//            ie.printStackTrace();
//        }
//    }
//
//    /**
//     * this method initialises the logger, creates an object
//     */
//    public static void initialize() {
//        logger = new Logger();
//    }
//// singleton - pattern
//    private static Logger logger;
//
//    public static Logger getLogger() {
//        return logger;
//    }
//}// End of class
//
/*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package org.fp.logger;
//
///*import java.util.logging.ErrorManager;
//import java.util.logging.Filter;
//import java.util.logging.Formatter;
//import java.util.logging.Handler;
//import java.util.logging.Level;
//import java.util.logging.LogManager;
//import java.util.logging.LogRecord;
//import java.util.logging.SimpleFormatter;*/
//import java.util.logging.*;
//
///**
// *
// * @author iroiro
// */
//
//public class WindowHandler extends Handler {
//    //window to which the logging is done to.
//    private LogWindow window = null;
//    private Formatter formatter = null;
//    private Level level = null;
//    private static WindowHandler handler = null; //the singleton instance
//    
//    /**
//     * private constructor, encapsulate and prevent initialization.
//     */
//    private WindowHandler(){
//        configure();
//        if (window == null){
//            window = new LogWindow("Logging window", 500, 200);
//        }
//    }
//    
//    /**
//     * The getInstance method returns the singleton instance of the
//     * WindowHandler object It is synchronized to prevent two threads trying to
//     * create an instance simultaneously.
//     * 
//     * @return WindowHandler object
//     */
//    public static synchronized WindowHandler getInstance(){
//        if (handler == null){
//            handler = new WindowHandler();
//        }
//        return handler;
//    }
//    
//    /**
//     * private method creating a formatter object with the formatter name. If
//     * no name is specified, it returns a SimpleFormatter object.
//     * 
//     * @parm formatterName the name of the formatter
//     * @retun Formatter object
//     */
//    private Formatter makeFormatter(String formatterName){
//        Class c = null;
//        Formatter f = null;
//        try{
//            c = Class.forName(formatterName);
//            f = (Formatter)c.newInstance();
//        }catch(Exception e){
//            System.out.println("Raise an exception, creating a SimpleFormatter "
//                    +"instead.");
//            f = new SimpleFormatter();
//        }
//        return f;       
//    }
//    
//    /**
//     * This is the overridden publish method of the abstract super class
//     * Handler. This method writes the logging information to the associated
//     * Java window. This method is synchronized to make it thread-safe. In case
//     * there's a problem, it reports the problme with the ErrorManager, only
//     * once and silently ignores the others.
//     * 
//     * @record the LogRecord object
//     */
//    public synchronized void publish(LogRecord record){
//        String message = null;
//        //check if the record is loggable
//        if (!isLoggable(record)){
//            return;
//        }
//        try{
//            message = getFormatter().format(record);
//        }catch(Exception e){
//            reportError(null, e, ErrorManager.FORMAT_FAILURE);
//        }
//        try{
//            window.showInfo(message);
//        }catch(Exception ex){
//            reportError(null, ex, ErrorManager.WRITE_FAILURE);
//        }        
//    }
//    
//    /**
//     * This method loads the configuration properties from the JDK level
//     * configuration file with the help of the LogManager class. It then sets
//     * its level, filter and formatter properties.
//     */
//    private void configure(){
//        LogManager manager = LogManager.getLogManager();
//        String className = this.getClass().getName();
//        String level = manager.getProperty(className+".level");
//        String filter = manager.getProperty(className+".filter");
//        String formatter = manager.getProperty(className+".formatter");
//        //accessing super class methods to set the parameters
//        setLevel(level != null ? Level.parse(level) : Level.INFO);
//        setFilter(makeFilter(filter));
//        setFormatter(makeFormatter(formatter));        
//    }
//    
//    /**
//     * private method constructing a filter object to a given filter name.
//     * @param filterName the name of the filter
//     * @return the filter object
//     */
//    private Filter makeFilter(String filterName){
//        Class c = null;
//        Filter f = null;
//        try{
//            c = Class.forName(filterName);
//            f = (Filter)c.newInstance();                        
//        }catch(Exception e){
//            System.out.println("There was a problem to load the filter class: "+
//                    filterName);
//        }
//        return f;            
//    }
//    
//    public void close(){}
//    
//
//    public void flush(){}
//
//    
//} //class WindowHandler














//
//public abstract class Jornal {
//
//    /**
//     * LoggerFactory for creating Loggers. By default, a JDK1.4 Jornal Factory
//     * will be used.
//     */
//    private static LoggerFactory loggerFactory = new Jdk4LoggerFactory();
//
//    /**
//     * Obtain a new or existing Jornal instance.
//     * 
//     * @param name Name of the logger, package names are recommended
//     */
//    public static Jornal getLogger(String name) {
//        return loggerFactory.getLogger(name);
//    }
//
//    /**
//     * Configures the logging system using properties in the supplied
//     * properties. Two properties are supported:
//     * <dl>
//     * <dt>logging.properties.file</dt>
//     * <dd>Configuration file. If the filename is prefixed by
//     * &quot;classpath:&quot;, the file must exist in the classpath, else it
//     * must exist on the specified location on the filesystem.</dd>
//     * <dt>logging.properties.type</dt>
//     * </dl>
//     * 
//     * @param properties Name of log properties with path.
//     */
//    public static void configure(Properties properties) {
//        String logFile = properties.getProperty("logging.properties.file",
//                "classpath:simpledbm.logging.properties");
//        Jornal.configureJDKLogging(logFile);
//
//    }
//
//    /**
//     * 
//     * 
//     */
//    static void configureJDKLogging(String name) {
//        loggerFactory = new Jdk4LoggerFactory();
//        final String classpathPrefix = "classpath:";
//        boolean searchClasspath = false;
//        String filename = name;
//        if (filename.startsWith(classpathPrefix)) {
//            filename = filename.substring(classpathPrefix.length());
//            searchClasspath = true;
//        }
//        InputStream is = null;
//        try {
//            if (searchClasspath) {
//                is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
//            } else {
//                is = new FileInputStream(filename);
//            }
//            java.util.logging.LogManager.getLogManager().readConfiguration(is);
//        } catch (Exception e) {
//            System.err.println("SIMPLEDBM-WL0001: Failed to initialize JDK 1.4 logging system due to following error:"
//                    + e.getMessage());
//        } finally {
//            try {
//                if (is != null) {
//                    is.close();
//                }
//            } catch (IOException e) {
//            }
//        }
//    }
//
//    public abstract void info(String sourceClass, String sourceMethod,
//            String message);
//
//    public abstract void info(String sourceClass, String sourceMethod,
//            String message, Throwable t);
//
//    public abstract void debug(String sourceClass, String sourceMethod,
//            String message);
//
//    public abstract void debug(String sourceClass, String sourceMethod,
//            String message, Throwable thrown);
//
//    public abstract void trace(String sourceClass, String sourceMethod,
//            String message);
//
//    public abstract void trace(String sourceClass, String sourceMethod,
//            String message, Throwable thrown);
//
//    public abstract void warn(String sourceClass, String sourceMethod,
//            String message);
//
//    public abstract void warn(String sourceClass, String sourceMethod,
//            String message, Throwable thrown);
//
//    public abstract void error(String sourceClass, String sourceMethod,
//            String message);
//
//    public abstract void error(String sourceClass, String sourceMethod,
//            String message, Throwable thrown);
//
//    public abstract boolean isTraceEnabled();
//
//    public abstract boolean isDebugEnabled();
//
//    public abstract void enableDebug();
//
//    public abstract void disableDebug();
//
//    static interface LoggerFactory {
//
//        Jornal getLogger(String name);
//    }
//
//    static class Jdk4LoggerFactory implements LoggerFactory {
//
//        public Jornal getLogger(String name) {
//            return new Jdk4Logger(name);
//        }
//    }
//
//    static final class Jdk4Logger extends Jornal {
//
//        private java.util.logging.Jornal realLogger;
//
//        public Jdk4Logger(String name) {
//            realLogger = java.util.logging.Jornal.getLogger(name);
//        }
//
//        public void info(String sourceClass, String sourceMethod, String message) {
//            realLogger.logp(java.util.logging.Level.INFO, sourceClass,
//                    sourceMethod, message);
//        }
//
//        public void info(String sourceClass, String sourceMethod,
//                String message, Throwable thrown) {
//            realLogger.logp(java.util.logging.Level.INFO, sourceClass,
//                    sourceMethod, message, thrown);
//        }
//
//        public void debug(String sourceClass, String sourceMethod,
//                String message) {
//            realLogger.logp(java.util.logging.Level.FINE, sourceClass,
//                    sourceMethod, message);
//        }
//
//        public void debug(String sourceClass, String sourceMethod,
//                String message, Throwable thrown) {
//            realLogger.logp(java.util.logging.Level.FINE, sourceClass,
//                    sourceMethod, message, thrown);
//        }
//
//        public void trace(String sourceClass, String sourceMethod,
//                String message) {
//            realLogger.logp(java.util.logging.Level.FINER, sourceClass,
//                    sourceMethod, message);
//        }
//
//        public void trace(String sourceClass, String sourceMethod,
//                String message, Throwable thrown) {
//            realLogger.logp(java.util.logging.Level.FINER, sourceClass,
//                    sourceMethod, message, thrown);
//        }
//
//        public void warn(String sourceClass, String sourceMethod, String message) {
//            realLogger.logp(java.util.logging.Level.WARNING, sourceClass,
//                    sourceMethod, message);
//        }
//
//        public void warn(String sourceClass, String sourceMethod,
//                String message, Throwable thrown) {
//            realLogger.logp(java.util.logging.Level.WARNING, sourceClass,
//                    sourceMethod, message, thrown);
//        }
//
//        public void error(String sourceClass, String sourceMethod,
//                String message) {
//            realLogger.logp(java.util.logging.Level.SEVERE, sourceClass,
//                    sourceMethod, message);
//        }
//
//        public void error(String sourceClass, String sourceMethod,
//                String message, Throwable thrown) {
//            realLogger.logp(java.util.logging.Level.SEVERE, sourceClass,
//                    sourceMethod, message, thrown);
//        }
//
//        public boolean isTraceEnabled() {
//            return realLogger.isLoggable(java.util.logging.Level.FINER);
//        }
//
//        public boolean isDebugEnabled() {
//            return realLogger.isLoggable(java.util.logging.Level.FINE)
//                    || realLogger.isLoggable(java.util.logging.Level.FINER);
//        }
//
//        public void enableDebug() {
//            realLogger.setLevel(java.util.logging.Level.FINE);
//        }
//
//        public void disableDebug() {
//            realLogger.setLevel(java.util.logging.Level.INFO);
//        }
//    }
//}